// pages/changePersionList/changePersionList.js
const app = getApp();
var request = require("../../utils/request.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userList: [],
        loading: false,
        uid: '',
        ischecked:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.setData({
            type: options.type,
            uid:options.uid
        })
        wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
    },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage (res) {
    return {
      title: '迪佳极视',
      path: 'pages/index/index',
      // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete:function(res){
        // 不管成功失败都会执行
      }
    }
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.toastDialog = this.selectComponent("#toastDialog");//此处id和页面的 wxml中的id一致
        this.queryPersionInfoList()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    choosePersion: function (e) {
        this.setData({ uid: e.detail.value })
    },
    //确定切换他人
    confirmCurrentUser: function () {
        console.log(this.data.uid)
        const that = this
        if (this.data.uid == '') {
            that.toastDialog.showDialog({ code: '1', message: '请选择用户', timeout: true }); //loading 加载框

            return false
        }
        // that.toastDialog.showDialog({ code: 'loading', message: '正在提交', timeout: false }); //loading 加载框
        // this.setData({ loading: true })
        // let data = {
        //     uid: this.data.uid,
        //     type: this.data.type
        // }
        wx.setStorage({
            key: 'uid',
            data: this.data.uid
            // data:"oLf5N5wQfXYroQkn_NVIV2dJCnC8"
          })
        // request.postData('xcx/testList', data, function (res) {
        //     // success
        //     that.setData({ loading: false })
        //     that.toastDialog.hideDialog(); //关闭loading提示框

        //     if (res.code == '0') {
        //         that.toastDialog.showDialog({ code: '0', message: '切换成功！', timeout: true });
                var pages = getCurrentPages();
                var prepages = pages[pages.length - 2];
                prepages.queryInfoList()
                wx.navigateBack({
                    delta: -1,
                })
        //         wx.setStorage({
        //             key: 'uid',
        //             data: res.openId
        //             // data:"oLf5N5wQfXYroQkn_NVIV2dJCnC8"
        //           })
        //     } else {
        //         that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });

        //     }
        // }, function (res) {
        //     // error
        //     that.setData({ loading: false })
        //     that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
        // });
    },
    //成员列表
    queryPersionInfoList: function () {
        let data = {}
        const that = this
        that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框
        request.postData('xcx/myUserList', data, function (res) {
            that.toastDialog.hideDialog(); //关闭loading提示框
            // success
            console.log(res);
            that.setData({ loading: false })
            if (res.code == '0') {
                for(var i = 0;i<res.userList.length;i++){
                    res.userList[i].ischecked = false;
                    if(res.userList[i].id == that.data.uid){
                        res.userList[i].ischecked = true;
                    }
                }
                that.setData({
                    userList: res.userList
                })
            } else {
                that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
            }

        }, function (res) {
            // error
            that.setData({ loading: false })
            that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
        });
    }
})